using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Windows.Forms;
using MEA.AE_Data;
using MEA.AE_Data.Config;

namespace MEA.AE_BusinessLogic
{
	public class FlashSelectV04 : Form
	{
		public enum EIgnoreFLCond
		{
			eNotIgnore,
			eIgnoreOneOfECU,
			eIgnoreBothOfECU
		}

		public enum EFlashingErrorCode
		{
			eNoErr,
			eAbbort,
			eNotEvoluated
		}

		public struct SFlashResult
		{
			public FlashBlock.EFD_Types fdType;

			public EFlashingErrorCode errCode;

			public SFlashResult(FlashBlock.EFD_Types type, EFlashingErrorCode err)
			{
				fdType = type;
				errCode = err;
			}
		}

		private const string cEcuConfNotFound = "This flash container file doesn't contain any data for updating this ECU!\nPlease abort the process and try with another FC-file.";

		private const string cNoEcuTypeEvoluated = "ECU Type cannot be determined!";

		private string sw_v;

		private string engType;

		private string hw_v;

		private ushort hw_TypeOfConectHW;

		private List<RadioButton> radioButtonList = new List<RadioButton>();

		private bool newECU;

		private FlashBlock.EFD_Types fdTypeOfECU = FlashBlock.EFD_Types.eFD_NoEFD_Type;

		private bool ecuConfFound;

		public string ecuConfErrStr = string.Empty;

		public EIgnoreFLCond ignoringFLCond;

		private List<FlashBlock.EFD_Types> listRadioButton;

		private IContainer components;

		private GroupBox groupBoxHW;

		private GroupBox groupBoxAF;

		private GroupBox groupBoxAA;

		private Button NextButton;

		private Button AbortButton;

		public event EventHandler ev_FlashFC;

		public void setFlashSelectV04(ref FlashContainer flCont, ushort hwTypeCon, string engineType, bool ignoringFLCond)
		{
			InitializeComponent();
			engType = engineType;
			sw_v = string.Format("{0}_{1}", flCont.Conditions_sw_v, flCont.Conditions_ds_v);
			hw_TypeOfConectHW = hwTypeCon;
			if (ignoringFLCond)
			{
				this.ignoringFLCond = EIgnoreFLCond.eIgnoreOneOfECU;
				if (hw_TypeOfConectHW == 0)
				{
					this.ignoringFLCond = EIgnoreFLCond.eIgnoreBothOfECU;
				}
			}
			if (ConfigData.HwRevTable.ContainsKey(hwTypeCon))
			{
				hw_v = (string)ConfigData.HwRevTable[hwTypeCon];
			}
			else
			{
				hw_v = string.Format("unknown ( \"{0:000}\") ", hwTypeCon);
			}
			if (hw_TypeOfConectHW > 5 && hw_TypeOfConectHW < 600)
			{
				newECU = true;
			}
			setLayoutFlCont(ref flCont.TypeList);
			if (ecuConfFound)
			{
				return;
			}
			ecuConfErrStr = "This flash container file doesn't contain any data for updating this ECU!\nPlease abort the process and try with another FC-file.";
			if (this.ignoringFLCond == EIgnoreFLCond.eIgnoreBothOfECU)
			{
				ecuConfErrStr = "ECU Type cannot be determined!";
			}
			else if (this.ignoringFLCond == EIgnoreFLCond.eNotIgnore)
			{
				SFlashResult sFlashResult = new SFlashResult(FlashBlock.EFD_Types.eFD_NoEFD_Type, EFlashingErrorCode.eNotEvoluated);
				if (this.ev_FlashFC != null)
				{
					this.ev_FlashFC(sFlashResult, null);
				}
			}
		}

		private void setLayoutFlCont(ref List<FlashBlock.EFD_Types> l)
		{
			listRadioButton = l;
			Point location = groupBoxAA.Location;
			int num = location.X;
			int num2 = location.Y;
			Size size = groupBoxAA.Size;
			FlashBlock flashBlock = new FlashBlock();
			int num3 = 0;
			int num4 = 0;
			int index = 2;
			for (int i = 2; i < l.Count; i++)
			{
				if (l[i] <= FlashBlock.EFD_Types.eFD_DS_2_AE_P)
				{
					num3++;
				}
				else if (l[i] > FlashBlock.EFD_Types.eFD_DS_2_AE_P)
				{
					num4++;
				}
			}
			Point location2 = new Point(num, num2);
			Size size2 = default(Size);
			Size size3 = default(Size);
			Point point = new Point(num, num2);
			RadioButton radioButton = new RadioButton();
			int num5 = radioButton.Size.Height;
			fdTypeOfECU = flashBlock.mkStr2ECUHWType(engType, newECU);
			if (num3 > 0)
			{
				groupBoxAA.Size = new Size(size.Width, num3 * num5 + num2 + num);
				for (int j = 0; j < num3; j++)
				{
					radioButton = new RadioButton();
					groupBoxAA.Controls.Add(radioButton);
					radioButton.Location = location2;
					location2 = new Point(location2.X, location2.Y + num5);
					radioButton.Text = flashBlock.mkECUHWTypeStr(l[index]);
					radioButton.Name = radioButton.Text + "AA";
					radioButton.Parent = groupBoxAA;
					radioButton.TabStop = false;
					radioButton.UseVisualStyleBackColor = true;
					if (fdTypeOfECU != l[index++] && ignoringFLCond == EIgnoreFLCond.eNotIgnore)
					{
						radioButton.Enabled = false;
					}
					else
					{
						radioButton.Click += radioButtomClicked;
						ecuConfFound = true;
					}
					radioButtonList.Add(radioButton);
				}
				size2 = groupBoxAA.Size;
				if (ignoringFLCond != EIgnoreFLCond.eIgnoreBothOfECU && newECU)
				{
					groupBoxAA.Enabled = false;
				}
				groupBoxAA.Parent = this;
				groupBoxHW.Controls.Add(groupBoxAA);
			}
			else
			{
				groupBoxAA.Hide();
			}
			location2 = new Point(num, num2);
			point = new Point(point.X, point.Y + size2.Height + num2);
			if (num4 > 0)
			{
				groupBoxAF.Size = new Size(size.Width, num4 * num5 + num2 + num);
				groupBoxAF.Location = point;
				for (int k = 0; k < num4; k++)
				{
					radioButton = new RadioButton();
					groupBoxAF.Controls.Add(radioButton);
					radioButton.Location = location2;
					location2 = new Point(location2.X, location2.Y + num5);
					radioButton.Text = flashBlock.mkECUHWTypeStr(l[index]);
					radioButton.Name = radioButton.Text + "AF";
					radioButton.Parent = groupBoxAF;
					radioButton.TabStop = false;
					radioButton.UseVisualStyleBackColor = true;
					if (fdTypeOfECU != l[index++] && ignoringFLCond == EIgnoreFLCond.eNotIgnore)
					{
						radioButton.Enabled = false;
					}
					else
					{
						radioButton.Click += radioButtomClicked;
						ecuConfFound = true;
					}
					groupBoxAF.Controls.Add(radioButton);
					radioButtonList.Add(radioButton);
				}
				if (ignoringFLCond != EIgnoreFLCond.eIgnoreBothOfECU && !newECU)
				{
					groupBoxAF.Enabled = false;
				}
				size3 = groupBoxAF.Size;
				groupBoxAF.Parent = this;
				groupBoxHW.Controls.Add(groupBoxAF);
			}
			else
			{
				groupBoxAF.Hide();
			}
			point = new Point(point.X, point.Y + size3.Height + num);
			AbortButton.Location = point;
			NextButton.Location = new Point(size.Width - AbortButton.Size.Width + num, AbortButton.Location.Y);
			NextButton.Size = AbortButton.Size;
			NextButton.Enabled = false;
			point = new Point(point.X, AbortButton.Location.Y + AbortButton.Size.Height + num);
			groupBoxHW.Controls.Add(AbortButton);
			groupBoxHW.Controls.Add(NextButton);
			groupBoxHW.Size = new Size(num2 + size.Width, point.Y);
			base.Controls.Add(groupBoxHW);
			base.Size = new Size(groupBoxHW.Width + num2, groupBoxHW.Size.Height + num2);
			size2 = (MaximumSize = base.Size);
			MinimumSize = size2;
			Focus();
			CenterToParent();
		}

		private void nextFlashButomClick(object sender, EventArgs e)
		{
			DialogResult dialogResult = MessageBox.Show(string.Format("Do you want to flash {0} to Engine {1},\nHW-Version {2}?", sw_v, engType, hw_v), "Selection Message", MessageBoxButtons.OKCancel, MessageBoxIcon.Question);
			if (dialogResult == DialogResult.OK)
			{
				SFlashResult sFlashResult = new SFlashResult(fdTypeOfECU, EFlashingErrorCode.eNoErr);
				if (this.ev_FlashFC != null)
				{
					this.ev_FlashFC(sFlashResult, null);
				}
				Dispose(true);
			}
		}

		private void radioButtomClicked(object sender, EventArgs e)
		{
			RadioButton radioButton = (RadioButton)sender;
			IntPtr handle = radioButton.Handle;
			if (radioButton.Checked)
			{
				for (int i = 0; i < radioButtonList.Count; i++)
				{
					RadioButton radioButton2 = radioButtonList[i];
					if (radioButton.Name == radioButton2.Name)
					{
						fdTypeOfECU = listRadioButton[i + 2];
					}
					else if (radioButton2.Checked)
					{
						radioButton2.Checked = false;
					}
				}
			}
			NextButton.Enabled = true;
		}

		private void abortFlashButtonClick(object sender, EventArgs e)
		{
			SFlashResult sFlashResult = new SFlashResult(FlashBlock.EFD_Types.eFD_NoEFD_Type, EFlashingErrorCode.eAbbort);
			if (this.ev_FlashFC != null)
			{
				this.ev_FlashFC(sFlashResult, null);
			}
			Dispose(true);
		}

		protected override void Dispose(bool disposing)
		{
			if (disposing && components != null)
			{
				components.Dispose();
			}
			base.Dispose(disposing);
		}

		private void InitializeComponent()
		{
			System.ComponentModel.ComponentResourceManager resources = new System.ComponentModel.ComponentResourceManager(typeof(MEA.AE_BusinessLogic.FlashSelectV04));
			this.groupBoxHW = new System.Windows.Forms.GroupBox();
			this.NextButton = new System.Windows.Forms.Button();
			this.AbortButton = new System.Windows.Forms.Button();
			this.groupBoxAF = new System.Windows.Forms.GroupBox();
			this.groupBoxAA = new System.Windows.Forms.GroupBox();
			this.groupBoxHW.SuspendLayout();
			base.SuspendLayout();
			this.groupBoxHW.Controls.Add(this.NextButton);
			this.groupBoxHW.Controls.Add(this.AbortButton);
			this.groupBoxHW.Controls.Add(this.groupBoxAF);
			this.groupBoxHW.Controls.Add(this.groupBoxAA);
			this.groupBoxHW.Location = new System.Drawing.Point(12, 12);
			this.groupBoxHW.Name = "groupBoxHW";
			this.groupBoxHW.Size = new System.Drawing.Size(200, 387);
			this.groupBoxHW.TabIndex = 0;
			this.groupBoxHW.TabStop = false;
			this.groupBoxHW.Text = "Select ECU HW-Version for flashing";
			this.NextButton.Location = new System.Drawing.Point(119, 337);
			this.NextButton.Name = "NextButton";
			this.NextButton.Size = new System.Drawing.Size(75, 23);
			this.NextButton.TabIndex = 1;
			this.NextButton.Text = "Next";
			this.NextButton.UseVisualStyleBackColor = true;
			this.NextButton.Click += new System.EventHandler(nextFlashButomClick);
			this.AbortButton.Location = new System.Drawing.Point(12, 336);
			this.AbortButton.Name = "AbortButton";
			this.AbortButton.Size = new System.Drawing.Size(60, 24);
			this.AbortButton.TabIndex = 2;
			this.AbortButton.Text = "Abort";
			this.AbortButton.UseVisualStyleBackColor = true;
			this.AbortButton.Click += new System.EventHandler(abortFlashButtonClick);
			this.groupBoxAF.Location = new System.Drawing.Point(12, 178);
			this.groupBoxAF.Name = "groupBoxAF";
			this.groupBoxAF.Size = new System.Drawing.Size(182, 127);
			this.groupBoxAF.TabIndex = 1;
			this.groupBoxAF.TabStop = false;
			this.groupBoxAF.Text = "Only AF";
			this.groupBoxAA.Location = new System.Drawing.Point(12, 24);
			this.groupBoxAA.Name = "groupBoxAA";
			this.groupBoxAA.Size = new System.Drawing.Size(182, 127);
			this.groupBoxAA.TabIndex = 0;
			this.groupBoxAA.TabStop = false;
			this.groupBoxAA.Text = "AA and AB, AC, AD, AE";
			base.AutoScaleDimensions = new System.Drawing.SizeF(6f, 13f);
			base.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
			this.AutoSize = true;
			base.ClientSize = new System.Drawing.Size(234, 411);
			base.ControlBox = false;
			base.Controls.Add(this.groupBoxHW);
			base.FormBorderStyle = System.Windows.Forms.FormBorderStyle.Fixed3D;
			base.Icon = (System.Drawing.Icon)resources.GetObject("$this.Icon");
			base.MaximizeBox = false;
			this.MaximumSize = new System.Drawing.Size(380, 480);
			base.MinimizeBox = false;
			this.MinimumSize = new System.Drawing.Size(240, 310);
			base.Name = "FlashSelectV04";
			this.Text = "Selection of FC-Area";
			base.TopMost = true;
			this.groupBoxHW.ResumeLayout(false);
			base.ResumeLayout(false);
		}
	}
}
